<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/3/22
 * Time: 20:51
 */

namespace app\common\model\mysql;


use think\Model;

class Article extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * 状态获取器
     * @param $value
     * @return string
     */
    public function getStatusAttr($value)
    {
        switch ($value) {
            case 1:
                return "发布";
            case 0:
                return "审核未通过";
            case 2:
                return "待审核";
            default:
                return "待审核";
        }
    }

    /**
     * 创建时间获取器
     * @param $value
     * @return false|string
     */
    public function getCreateTimeAttr($value)
    {
        return date("Y-m-d H:i:s", $value);
    }

    /**
     * 查询所有文章
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList()
    {
        return $this->select();
    }

    /**
     * 根据id查询一个文章详情信息
     * @param $id
     * @return bool|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneArticleById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }

        // 获取原始数据
        return $this->find($id)->getData();
    }

    public function deleteById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }

        return $this->where('id', $id)->delete();
    }
}